package com.sl.ms.work.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.ms.base.api.common.MQFeign;
import com.sl.ms.oms.api.OrderFeign;
import com.sl.ms.oms.dto.OrderCargoDTO;
import com.sl.ms.oms.dto.OrderDetailDTO;
import com.sl.ms.oms.dto.OrderLocationDTO;
import com.sl.ms.transport.api.TransportLineFeign;
import com.sl.ms.work.domain.enums.WorkExceptionEnum;
import com.sl.ms.work.domain.enums.pickupDispatchtask.PickupDispatchTaskType;
import com.sl.ms.work.domain.enums.transportorder.TransportOrderSchedulingStatus;
import com.sl.ms.work.domain.enums.transportorder.TransportOrderStatus;
import com.sl.ms.work.entity.TransportOrderEntity;
import com.sl.ms.work.entity.TransportOrderTaskEntity;
import com.sl.ms.work.mapper.TransportOrderTaskMapper;
import com.sl.ms.work.service.TransportOrderTaskService;
import com.sl.transport.common.constant.Constants;
import com.sl.transport.common.enums.IdEnum;
import com.sl.transport.common.exception.SLException;
import com.sl.transport.common.service.IdService;
import com.sl.transport.common.vo.OrderMsg;
import com.sl.transport.common.vo.TransportOrderMsg;
import com.sl.transport.common.vo.TransportOrderStatusMsg;
import com.sl.transport.domain.TransportLineNodeDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 运单表 服务实现类
 */
@Service
public class TransportOrderTaskServiceImpl extends
        ServiceImpl<TransportOrderTaskMapper, TransportOrderTaskEntity> implements TransportOrderTaskService {



    @Override
    public void batchSaveTransportOrder(List<TransportOrderTaskEntity> transportOrderTaskList) {
        saveBatch(transportOrderTaskList);
    }

    @Override
    public IPage<TransportOrderTaskEntity> findByPage(Integer page, Integer pageSize, String transportOrderId, Long transportTaskId) {
        Page<TransportOrderTaskEntity> iPage = new Page(page, pageSize);
        LambdaQueryWrapper<TransportOrderTaskEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(transportOrderId), TransportOrderTaskEntity::getTransportOrderId, transportOrderId);
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(transportTaskId), TransportOrderTaskEntity::getTransportTaskId, transportTaskId);
        return super.page(iPage, lambdaQueryWrapper);
    }

    @Override
    public List<TransportOrderTaskEntity> findAll(String transportOrderId, Long transportTaskId) {
        LambdaQueryWrapper<TransportOrderTaskEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(transportOrderId), TransportOrderTaskEntity::getTransportOrderId, transportOrderId);
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(transportTaskId), TransportOrderTaskEntity::getTransportTaskId, transportTaskId);
        lambdaQueryWrapper.orderBy(true, false, TransportOrderTaskEntity::getCreated);
        return list(lambdaQueryWrapper);
    }

    @Override
    public Long count(String transportOrderId, Long transportTaskId) {
        LambdaQueryWrapper<TransportOrderTaskEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(transportOrderId), TransportOrderTaskEntity::getTransportOrderId, transportOrderId);
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(transportTaskId), TransportOrderTaskEntity::getTransportTaskId, transportTaskId);
        return super.count(lambdaQueryWrapper);
    }

    @Override
    public void del(String transportOrderId, Long transportTaskId) {
        if (ObjectUtil.isAllEmpty(transportOrderId, transportTaskId)) {
            return;
        }
        
        LambdaQueryWrapper<TransportOrderTaskEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(transportOrderId), TransportOrderTaskEntity::getTransportOrderId, transportOrderId);
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(transportTaskId), TransportOrderTaskEntity::getTransportTaskId, transportTaskId);

        super.remove(lambdaQueryWrapper);
    }

}
